package service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import models.Book;
import models.Borrow;
import models.Indent;
import models.LendCard;
import models.LendCardF;
import models.Member;

public class BorrowService {
	
	public static Indent findborrow (String id){
		Indent indlist = Indent.find("from Indent where id = ? ", Long.parseLong(id)).first();
		return indlist;
	}
	
	public static Book findbook(String id){
		Book book = null ; 
		try{
			 book = Book.findById(Long.parseLong(id));
		}catch(Exception e){
			e.printStackTrace();
		}
		return book;
	}
	
	public static LendCardF countDay(String uid){
		Member member = Member.findById(Long.parseLong(uid));
		LendCard lendcard = LendCard.find("from LendCard where lecnum = ?", member.lecnum).first();
		LendCardF lendcardf = LendCardF.find("from LendCardF where money <= ?", 50.00).first();
		return  lendcardf ;
	}
	
	public static int cday(Date tday,String uid,String indnum){
		int day = 0 ;
		 Date date_start;
		 Date date_end;
		 try {
			SimpleDateFormat   formatter   =   new   SimpleDateFormat   ("yyyy-MM-dd HH:mm:ss");   
			Date   curDate   =   new   Date(System.currentTimeMillis());//获取当前时间   
			String   starttime   =   formatter.format(curDate);
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	        java.util.Date sdtime = format.parse(starttime);
	      
	         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			 date_start = sdf.parse(formatter.format(tday));
			 date_end = sdf.parse(formatter.format(sdtime));
			 Calendar cal_start = Calendar.getInstance();
			 Calendar cal_end = Calendar.getInstance();
			 cal_start.setTime(date_start);
			 cal_end.setTime(date_end);
			 day = (int) ((date_end.getTime()-date_start.getTime())/(24*60*60*1000));
			 System.out.println(day+"---------------------d----d-d-d----dddd-");
			 Member member = Member.findById(Long.parseLong(uid));
			 LendCard lendcard = LendCard.find("from LendCard where lecnum = ?", member.lecnum).first();
			 LendCardF lendcardf = LendCardF.find("from LendCardF where money = ?", lendcard.lecmone).first();
			 day = lendcardf.date - day ;
			 System.out.println(day+"---------------------d----d-d-d----dddd-");
			 Indent indent = Indent.find("from Indent where indnum = ?", indnum).first();
			 if(indent.indsta == 6 ){
				 if(day >0){
					 day = (lendcardf.maxDate - lendcardf.date) + day ;
					 System.out.println(day+"-------------------ddddddddddddddddd");
					 return day ;
				 }
				 if(day<0){
					 day = Math.abs(day);
					 day = (lendcardf.maxDate - lendcardf.date) - day ;
					 System.out.println(day+"-------------cccccccccccc------------");
					 return day ;
				 }
			 }
		}catch(Exception e){
			e.printStackTrace();
		}
		return day;
	}
	
	public static String time(Date timeday){
		SimpleDateFormat   formatter   =   new   SimpleDateFormat   ("yyyy-MM-dd HH:mm:ss");
		String tt = formatter.format(timeday);
		return tt ;
	}
}
